//
//  DECXDriverAddViewController.m
//  DECXInStore
//
//  Created by 李坤 on 2018/9/7.
//  Copyright © 2018年 程强. All rights reserved.
//

#import "DECXDriverAddViewController.h"
#import "DECXManager.h"
#import "DECXWarehouseChargeManager.h"

@interface DECXDriverAddViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) DECXManager *manager;
@property (strong, nonatomic) DECXWarehouseChargeManager *wManager;
@property (weak, nonatomic) IBOutlet UITextField *nameTxf;
@property (weak, nonatomic) IBOutlet UITextField *phoneTxf;
@property (weak, nonatomic) IBOutlet UITextField *idCardTxf;
@property (weak, nonatomic) IBOutlet UITextField *carNoTxf;
@end

@implementation DECXDriverAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DECXManager new];
    self.wManager = [DECXWarehouseChargeManager new];
    [self setRightBarItem];
}
- (void)setRightBarItem
{
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(commitClick)];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)commitClick
{
    [self resignKeyboard];
    if (![self checkInput]) {
        return;
    }
    weak_Self(self);
    if ([DECXGlobal shareInstance].loginType.integerValue == 2) {
        [self performBlock:^{
            [weakSelf decxUserAddDriver];
        } delay:0.5];
    }else{
        [self performBlock:^{
            [weakSelf addDriver];
        } delay:0.5];
    }
}
- (BOOL)checkInput
{
    if (self.nameTxf.text.length == 0) {
        ToastShowInWindow(@"请填写司机姓名");
        return NO;
    }
    if (![NOValidateHelper validateMobile:self.phoneTxf.text]) {
        ToastShowInWindow(@"请填写正确的手机号");
        return NO;
    }
    if (self.carNoTxf.text.length == 0) {
        ToastShowInWindow(@"请填写车牌号");
        return NO;
    }
    return YES;
}
#pragma mark ---- 仓管用户添加司机
- (void)addDriver
{
    MyUser;
    weak_Self(self);
    [[self.manager addDriver:self.nameTxf.text phone:self.phoneTxf.text carNo:self.carNoTxf.text warehouse:user.depot_id idCard:self.idCardTxf.text]subscribeNext:^(id x) {
        ToastShowInWindow(@"添加成功");
        [weakSelf gotoBack];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        
    }];
}
#pragma mark --- 席位号用户添加司机
- (void)decxUserAddDriver
{
    MyUser;
    weak_Self(self);
    [[self.wManager addDriverWithUid:user.userid name:self.nameTxf.text phone:self.phoneTxf.text car_code:self.carNoTxf.text card_no:self.idCardTxf.text]subscribeNext:^(id x) {
        ToastShowInWindow(@"添加成功");
        [weakSelf gotoBack];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        ToastShowInWindow(msg);
    } completed:^{
        
    }];
}
- (void)resignKeyboard
{
    [self.nameTxf resignFirstResponder];
    [self.phoneTxf resignFirstResponder];
    [self.idCardTxf resignFirstResponder];
    [self.carNoTxf resignFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
